import  pymysql, datetime
import requests
from crawlab import save_item
settings = { 'host':'172.16.10.187','user':'tidb','port':3306,'password':'Tidb@guest@123','database':'crawl','charset' : 'utf8'}
conn = pymysql.connect(**settings)

sql = '''insert into flower_trading_tendency(url,timeString,timeformat,avgPrice,highestPrice,create_time)values '''\
                        '''(%s,%s,%s,%s,%s,%s)'''

cursor=conn.cursor()


if __name__ == '__main__':
    params = {'method':'getInfo','breed':111}
    url = 'http://www.kifa.net.cn/breedQuotation.do?method=getInfo&breed=111'
    req = requests.get(url=url,params=params)
    data = req.json()
    for i in data:
        i['url'] = url
        timeString = i.get('timeString')
        i['timeformat'] = timeString[:4]+'年'+timeString[4:6]+'月'+timeString[-2:]+'日'
        day = timeString[-2:]
        i['create_time'] = datetime.datetime.now()
        row = (i.get('url'),i.get('timeString'),i.get('timeformat'),i.get('avgPrice'),i.get('highestPrice'),i.get('create_time'))
        cursor.execute(sql,row)
        conn.commit()
        save_item(i)

cursor.close()
conn.close()
req.close()